﻿// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//  MockAttachment.cs
//    
// Abstract:
//
//  Implements Microsoft.Office.Interop.Outlook.Attachment 
//  and Microsoft.Office.Interop.Outlook.Attachments interfaces 
//  for unit testing
//  
//--

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Office.Interop.Outlook;
using System.Runtime.Serialization.Formatters.Binary;

namespace Microsoft.EnhancedReminders.OutlookAddIn.UnitTests
{
    /// <summary>
    /// Implements Microsoft.Office.Interop.Outlook.Attachments interface for unit testing
    /// </summary>
    [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface",
        Justification = "Mock object implementing Outlook interface")]
    [SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix",
        Justification = "Mock object implementing Outlook interface")]
    public sealed class MockAttachments : Attachments
    {
        private Type _type;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="type"></param>
        public MockAttachments(Type type)
        {
            _type = type;
        }

        private List<Attachment> _attachments = new List<Attachment>();

        #region Attachments Members

        Attachment Attachments.Add(object Source, object Type, object Position, object DisplayName)
        {
            MockAttachment attachment;

            FileStream reader;

            using (reader = new FileStream(Source.ToString(), FileMode.Open))
            {
                // Deserialize the attachment
                BinaryFormatter serializer = new BinaryFormatter();
                Debug.Assert(serializer != null, "serializer != null");

                attachment = new MockAttachment(serializer.Deserialize(reader), _attachments);
                Debug.Assert(attachment != null, "attachment != null");
            }

            _attachments.Add(attachment);

            return attachment as Attachment;
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
    Justification = "Properties not implemented in Mock object")]
        Application Attachments.Application
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
    Justification = "Properties not implemented in Mock object")]
        OlObjectClass Attachments.Class
        {
            get { throw new System.NotImplementedException(); }
        }

        int Attachments.Count
        {
            get 
            { 
                return _attachments.Count; 
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
    Justification = "Properties not implemented in Mock object")]
        object Attachments.Parent
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
    Justification = "Properties not implemented in Mock object")]
        void Attachments.Remove(int Index)
        {
            throw new System.NotImplementedException();
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
    Justification = "Properties not implemented in Mock object")]
        NameSpace Attachments.Session
        {
            get { throw new System.NotImplementedException(); }
        }

        Attachment Attachments.this[object Index]
        {
            get 
            {
                int index;
                if (Int32.TryParse(Index.ToString(), out index))
                {
                    return _attachments[index];
                }
                else
                {
                    return null;
                }
            }

        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _attachments.GetEnumerator();
        }

        #endregion
    }

    /// <summary>
    /// Implements Microsoft.Office.Interop.Outlook.Attachment interface for unit testing
    /// </summary>
    public sealed class MockAttachment : Attachment
    {
        private object _serializedObject;
        private List<Attachment> _attachments;

        /// <summary>
        /// Overloaded constructor that takes an object which is streamed into the file 
        /// specified by the SaveAsFile parameter
        /// </summary>
        /// <param name="value"></param>
        /// <param name="attachments"></param>
        public MockAttachment(object value, List<Attachment> attachments)
        {
            Debug.Assert(value != null, "serializedObject != null");
            Debug.Assert(attachments != null, "attachments != null");

            _serializedObject = value;
            _attachments = attachments;
        }

        #region Attachment Members

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        Application Attachment.Application
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        OlAttachmentBlockLevel Attachment.BlockLevel
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        OlObjectClass Attachment.Class
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        void Attachment.Delete()
        {
            _attachments.Remove(this);
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        string Attachment.DisplayName
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
                throw new System.NotImplementedException();
            }
        }

        string Attachment.FileName
        {
            get 
            { 
                return Constants.AttachmentName; 
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        int Attachment.Index
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        object Attachment.MAPIOBJECT
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        object Attachment.Parent
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        string Attachment.PathName
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        int Attachment.Position
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
                throw new System.NotImplementedException();
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        PropertyAccessor Attachment.PropertyAccessor
        {
            get { throw new System.NotImplementedException(); }
        }

        void Attachment.SaveAsFile(string Path)
        {
            using (FileStream writer = new FileStream(Path, FileMode.Create))
            {
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.Serialize(writer, _serializedObject);
            }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        NameSpace Attachment.Session
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
            Justification = "Properties not implemented in Mock object")]
        int Attachment.Size
        {
            get { throw new System.NotImplementedException(); }
        }

        [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations",
           Justification = "Properties not implemented in Mock object")]
        OlAttachmentType Attachment.Type
        {
            get { throw new System.NotImplementedException(); }
        }

        #endregion
    }
}